home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991: Code Warrior / bincue / Code Warrior.bin / Developer Essentials / DTS Sample Code / Snippets / Snippets Aug '91 / FreqForEverChange / FreqForEverChange.c next >
Encoding:
C/C++ Source or Header  |  1991-09-09  |  12.9 KB  |  535 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <Windows.h>
  11. #include    <OSUtils.h>
  12. #include    <Controls.h>
  13. #include    <desk.h>
  14. #include    <dialogs.h>
  15. #include    <Events.h>
  16. #include    <Memory.h>
  17. #include    <Menus.h>
  18. #include    <Notification.h>
  19. #include    <OSEvents.h>
  20. #include    <Packages.h>
  21. #include    <Palette.h>
  22. #include    <Printing.h>
  23. #include    <Files.h>
  24. #include    <Resources.h>
  25. #include    <SANE.h>
  26. #include    <SegLoad.h>
  27. #include    <Sound.h>
  28. #include    <SoundInput.h>
  29. #include    <Slots.h>
  30. #include    <ToolUtils.h>
  31. #include    <StdIO.h>
  32. #include    <math.h>
  33. #include    <StandardFile.h>
  34.  
  35. extern _DataInit();
  36.  
  37. #define    TRUE            0xFF
  38. #define    FALSE            0
  39. #define    VERSION            1
  40. #define    SR_BIT            0
  41.  
  42. #define    appleID            128            
  43. #define    appleMenu        0
  44. #define    aboutMeCommand    1
  45.  
  46. #define    fileID            129
  47. #define    startCommand    1
  48. #define    printCommand    3
  49. #define    quitCommand     5
  50.  
  51. #define    editID            130
  52.  
  53. #define    aboutMeDLOG        128
  54. #define    okButton        1
  55. #define    authorItem        2
  56. #define    languageItem    3
  57.  
  58. long                Tick, WhereBoard;
  59. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  60. Rect                screenRect, BaseRect, TotalRect, minRect;
  61. WindowPtr            whichWindow, myWindow;
  62. CTabHandle            mycolors;
  63. PaletteHandle        srcPalette;
  64. MenuHandle            mymenu1, mymenu2, mymenu0;
  65. EventRecord         myEvent;
  66. Boolean                DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
  67. GDHandle            theGDevice;
  68. SysEnvRec            theWorld;
  69. OSErr                OSys;
  70. SpBlockPtr            mySpBlockPtr;
  71. SpBlock                mySpBlock;
  72.     
  73. /*______________________________________________________*/
  74. /*                What ever You want                    */
  75. /*______________________________________________________*/
  76. void start()
  77. {
  78.     Handle            SoundData;
  79.     SndChannelPtr    chan;
  80.     OSErr            err;
  81.     SndCommand        mycmd;
  82.     KeyMap            myKeys;
  83.     
  84.     SoundData = GetResource ('snd ', 100);
  85.     if (ResError() != noErr || SoundData == nil)
  86.         Debugger();
  87.     HLock (SoundData);
  88.             
  89.     mycmd.cmd = soundCmd;
  90.     mycmd.param1 = 0;
  91.     mycmd.param2 = (long) *SoundData;
  92.     
  93.     chan = nil;
  94.     err = SndNewChannel (&chan, sampledSynth, 0, nil);
  95.     if (err != noErr)
  96.         Debugger();
  97.     
  98.     err = SndDoImmediate (chan, &mycmd);
  99.     if (err != noErr)
  100.         Debugger();
  101.  
  102.     mycmd.cmd = freqCmd;
  103.     mycmd.param1 = 0;
  104.     mycmd.param2 = 60;
  105.     
  106.     err = SndDoCommand (chan, &mycmd, FALSE);
  107.     if (err != noErr)
  108.         Debugger();
  109.  
  110.     do {
  111.         GetKeys (myKeys);
  112.         if (((myKeys[3] >> 6) & 1) != FALSE) {
  113.             mycmd.cmd = freqCmd;
  114.             mycmd.param1 = 0;
  115.             mycmd.param2 += 1;
  116.     
  117.             err = SndDoImmediate (chan, &mycmd);
  118.             if (err != noErr)
  119.                 Debugger();
  120.             }
  121.             
  122.         if (((myKeys[3] >> 5) & 1) != FALSE) {
  123.             mycmd.cmd = freqCmd;
  124.             mycmd.param1 = 0;
  125.             mycmd.param2 -= 1;
  126.     
  127.             err = SndDoImmediate (chan, &mycmd);
  128.             if (err != noErr)
  129.                 Debugger();
  130.             }
  131.             
  132.         } while (((myKeys[0] >> 20) & 1) == FALSE);
  133.     
  134.     mycmd.cmd = quietCmd;
  135.     mycmd.param1 = 0;
  136.     mycmd.param2 = 0;
  137.     
  138.     err = SndDoImmediate (chan, &mycmd);
  139.     if (err != noErr)
  140.         Debugger();
  141.  
  142.     err = SndDisposeChannel (chan,FALSE);
  143.     if (err != noErr)
  144.         Debugger();
  145.         
  146.     HUnlock (SoundData);
  147.     return;
  148. }
  149.  
  150. /*______________________________________________________*/
  151. /*                     Draw Stuff                       */
  152. /*______________________________________________________*/
  153. void DrawStuff (theRect)
  154.  
  155.     Rect         theRect;
  156. {
  157.     SetRect (&theRect, 10, 10, 200, 200);
  158.     
  159.     CopyBits (&qd.screenBits, &qd.thePort->portBits, &qd.screenBits.bounds, 
  160.                 &qd.screenBits.bounds, srcCopy, nil);
  161.  
  162.  
  163. /**
  164.     CopyBits (&window->portBits, &window->portBits, &window->portRect, &window->portRect, srcOr, nil);
  165.     CopyBits (&windpeek->port.portBits, &windpeek->port.portBits, &window->portRect, &window->portRect, srcOr, nil);
  166.     CopyBits (&screenBits, &(*thePort).portBits, &theWorld, &theWorld, srcOr, nil);
  167. */
  168.     return;
  169. }  /* DrawStuff */
  170.  
  171. /*______________________________________________________*/
  172. /*                        Print                         */
  173. /*______________________________________________________*/
  174. void Print()
  175. {
  176.     TPPrPort    thePrPort;
  177.     TPrStatus    theStatus;
  178.     GrafPtr        oldPort;
  179.     THPrint        thePrRecHdl;
  180.     
  181.     GetPort(&oldPort);
  182.     
  183.     thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
  184.     
  185.     PrOpen();
  186.  
  187.         if (PrError() == noErr)
  188.          {
  189.             PrintDefault(thePrRecHdl);
  190.             
  191.             if (! PrStlDialog(thePrRecHdl)) 
  192.               ExitToShell();
  193.               
  194.             if (! PrJobDialog(thePrRecHdl)) 
  195.               ExitToShell();
  196.               
  197.             thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
  198.         
  199.             if (PrError() == noErr) 
  200.              {
  201.                 PrOpenPage(thePrPort, nil);
  202.                 if (PrError() == noErr) 
  203.                  {
  204.                     /********* Print from here ****************/
  205.                     
  206.                     DrawStuff ((**thePrRecHdl).prInfo.rPage);
  207.                  }
  208.              }
  209.             PrClosePage(thePrPort);
  210.         }
  211.         PrCloseDoc(thePrPort);
  212.     
  213.     if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  214.         PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
  215.  
  216.     PrClose();
  217.  
  218.     SetPort(oldPort);
  219.     return;
  220. }
  221.  
  222. /*______________________________________________________*/
  223. /*                 About Prog Dialog                    */
  224. /*______________________________________________________*/
  225. void showAboutMeDialog()
  226. {
  227.     GrafPtr     savePort;
  228.     DialogPtr    theDialog;
  229.     short        itemHit;
  230.  
  231.     GetPort(&savePort);
  232.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  233.     SetPort(theDialog);
  234.  
  235.     do {
  236.         ModalDialog(nil, &itemHit);
  237.     } while (itemHit != okButton);
  238.  
  239.     CloseDialog(theDialog);
  240.  
  241.     SetPort(savePort);
  242.     return;
  243. }
  244.  
  245. /*______________________________________________________*/
  246. /*                 Do Menu Function                     */
  247. /*______________________________________________________*/
  248. void doCommand(mResult)
  249.     long    mResult;
  250. {
  251.     int                     theMenu, theItem;
  252.     char                    daName[256];
  253.     GrafPtr                 savePort;
  254.  
  255.     theItem = LoWord(mResult);
  256.     theMenu = HiWord(mResult);
  257.     
  258.     switch (theMenu) {
  259. /*______________________________________________________*/
  260. /*                    Do Apple Menu                     */
  261. /*______________________________________________________*/
  262.         case appleID:
  263.             if (theItem == aboutMeCommand)
  264.                 showAboutMeDialog();
  265.             else {
  266.                 GetItem(mymenu0, theItem, daName);
  267.                 GetPort(&savePort);
  268.                 (void) OpenDeskAcc(daName);
  269.                 SetPort(savePort);
  270.             }
  271.             break;
  272. /*______________________________________________________*/
  273. /*                     Do File Menu                     */
  274. /*______________________________________________________*/
  275.         case fileID:
  276.             switch (theItem) {
  277.                 case startCommand:
  278.                     start();
  279.                     break;
  280.                 case printCommand:
  281.                     Print();
  282.                     break;
  283.                 case quitCommand:
  284.                     DoneFlag = TRUE;
  285.                     break;
  286.                 default:
  287.                     break;
  288.                 }
  289.             break;
  290. /*______________________________________________________*/
  291. /*                     Do Edit Menu                     */
  292. /*______________________________________________________*/
  293.         case editID:
  294.             switch (theItem) {
  295.                 default:
  296.                     break;
  297.                 }
  298.             break;
  299.         default:
  300.             break;
  301.         }
  302.     HiliteMenu(0);
  303.     return;
  304. }
  305.  
  306. /*______________________________________________________*/
  307. /*               Initialization traps                   */
  308. /*______________________________________________________*/
  309. void init()
  310. {
  311.     RgnHandle        tempRgn;
  312.     
  313.     UnloadSeg(_DataInit);
  314.     InitGraf(&qd.thePort);
  315.     FlushEvents(everyEvent, 0);
  316.     InitWindows();
  317.     InitDialogs(nil);
  318.     InitCursor();
  319.     MaxApplZone();
  320. /*______________________________________________________*/
  321. /*            If not right Machine then stop            */
  322. /*______________________________________________________*/
  323. /*    OSys = SysEnvirons(VERSION,&theWorld);
  324.     if(!theWorld.hasColorQD)
  325.         Color = TRUE;
  326.     else
  327.         Color = FALSE;
  328.         
  329.     if(theWorld.processor >= env68020)
  330.         CPU20 = TRUE;
  331.     else
  332.         CPU20 = FALSE;
  333.         
  334.     if(!theWorld.hasFPU)
  335.         FPU = TRUE;
  336.     else
  337.         FPU = FALSE;
  338.         
  339.     if(theWorld.systemVersion < 0x0600)
  340.         Sys6 = TRUE;
  341.     else
  342.         Sys6 = FALSE;
  343.         
  344.     if (CPU20 && (GetTrapAddress(0xA06E) != GetTrapAddress(0xA89F))) {
  345.         mySpBlockPtr = &mySpBlock;
  346.         mySpBlockPtr->spSlot = 0;
  347.         mySpBlockPtr->spTBMask = 0x03;
  348.         mySpBlockPtr->spCategory = 9;
  349.         mySpBlockPtr->spCType = 2;
  350.                 
  351.         err = SNextTypeSRsrc (mySpBlockPtr);
  352.                 
  353.         if (err) {
  354.             mySpBlockPtr = &mySpBlock;
  355.             mySpBlockPtr->spSlot = 0;
  356.             mySpBlockPtr->spTBMask = 0x03;
  357.             mySpBlockPtr->spCategory = 9;
  358.             mySpBlockPtr->spCType = F;
  359.                 
  360.             err = SNextTypeSRsrc (mySpBlockPtr);
  361.                 
  362.             if (err)
  363.                 Memboard = FALSE;
  364.             else {
  365.                 Memboard = TRUE;
  366.                 WhereBoard = mySpBlockPtr->spSlot;
  367.                 }
  368.             }
  369.         else {
  370.             Memboard = TRUE;
  371.             WhereBoard = mySpBlockPtr->spSlot;
  372.             }
  373.         }*/
  374.                     
  375. /*______________________________________________________*/
  376. /*                     Set Rects                        */
  377. /*______________________________________________________*/
  378.     screenRect = qd.screenBits.bounds;
  379.     offLeft = 0;
  380.     offTop = 0;
  381.     offRight = screenRect.right;
  382.     offBottom = screenRect.bottom;
  383.     SetRect(&BaseRect, 40, 60, 472, 282);
  384.     tempRgn = GetGrayRgn();
  385.     HLock ((Handle) tempRgn);
  386.     TotalRect = (**tempRgn).rgnBBox;
  387.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  388.                 (**tempRgn).rgnBBox.bottom - 40);
  389.     HUnlock ((Handle) tempRgn);
  390.  
  391. /*______________________________________________________*/
  392. /*        Open Window & set Palette & Picture           */
  393. /*______________________________________________________*/
  394.     theGDevice = GetMainDevice();
  395.     HLock ((Handle) theGDevice);
  396.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  397.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  398.     HUnlock((Handle) theGDevice);
  399.     switch (numcolor) {
  400.         case 1:
  401.             numcolor = 2;
  402.             break;
  403.         case 2:
  404.             numcolor = 4;
  405.             break;
  406.         case 4:
  407.             numcolor = 16;
  408.             break;
  409.         case 8:
  410.             numcolor = 256;
  411.             break;
  412.         }
  413.     
  414.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  415.                             (WindowPtr) -1, TRUE, 150);
  416.     SetPort((WindowPtr) myWindow);
  417.     DrawGrowIcon (myWindow);
  418.  
  419.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  420.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  421.     
  422. /*______________________________________________________*/
  423. /*                    Set menus                         */
  424. /*______________________________________________________*/
  425.     mymenu0 = GetMenu(appleID);
  426.     AddResMenu(mymenu0, 'DRVR');
  427.     InsertMenu(mymenu0,0);
  428.     mymenu1 = newmenu(129,"File");
  429.     appendmenu(mymenu1,"Start;-;Print;-;Quit");
  430.     InsertMenu(mymenu1,0);
  431.     mymenu2 = newmenu(130,"Edit");
  432.     InsertMenu(mymenu2,0);
  433.     DrawMenuBar();
  434.  
  435. /*______________________________________________________*/
  436. /*                  Init variables                      */
  437. /*______________________________________________________*/
  438.     DoneFlag = FALSE;
  439.     yieldTime = 0;
  440.     return;
  441. }
  442.  
  443. main()
  444. {
  445.     Boolean        track;
  446.     long        growResult;
  447. /*______________________________________________________*/
  448. /*                   Main Event loop                    */
  449. /*______________________________________________________*/
  450.     init();
  451.     for ( ;; ) {
  452.         if (DoneFlag) {
  453.             ExitToShell();
  454.             }
  455.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  456.             switch (myEvent.what) {
  457.                 case mouseDown:
  458.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  459.                         case inSysWindow:
  460.                             SystemClick(&myEvent, whichWindow);
  461.                             break;
  462.                         case inMenuBar:
  463.                             doCommand(MenuSelect(myEvent.where));
  464.                             break;
  465.                         case inContent:
  466.                             break;
  467.                         case inDrag:
  468.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  469.                             EraseRect (&whichWindow->portRect);
  470.                             DrawGrowIcon (whichWindow);
  471.                             break;
  472.                         case inGrow:
  473.                             growResult = GrowWindow (whichWindow, myEvent.where,
  474.                                                     &minRect);
  475.                             SizeWindow(whichWindow, LoWord(growResult), 
  476.                                     HiWord(growResult), TRUE);
  477.                             EraseRect (&whichWindow->portRect);
  478.                             DrawGrowIcon (whichWindow);
  479.                             break;
  480.                         case inGoAway:
  481.                             track = TrackGoAway (whichWindow, myEvent.where);
  482.                             if (track)
  483.                                 CloseWindow (whichWindow);
  484.                             break;
  485.                         case inZoomIn:
  486.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  487.                             if (track) {
  488.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  489.                                 EraseRect (&whichWindow->portRect);
  490.                                 DrawGrowIcon (whichWindow);
  491.                                 }
  492.                             break;
  493.                         case inZoomOut:
  494.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  495.                             if (track) {
  496.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  497.                                 EraseRect (&whichWindow->portRect);
  498.                                 DrawGrowIcon (whichWindow);
  499.                                 }
  500.                             break;
  501.                         default:
  502.                             break;
  503.                         }
  504.                     break;
  505.                 case keyDown:
  506.                     break;
  507.                 case autoKey:
  508.                     break;
  509.                 case updateEvt:
  510.                     if ((WindowPtr) myEvent.message == myWindow) {
  511.                         BeginUpdate((WindowPtr) myWindow);
  512.                         EndUpdate((WindowPtr) myWindow);
  513.                         }
  514.                     break;
  515.                 case diskEvt:
  516.                     break;
  517.                 case activateEvt:
  518.                     break;
  519.                 case app4Evt:
  520.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  521.                         yieldTime = 30;
  522.                         HideWindow((WindowPtr) myWindow);
  523.                         }
  524.                     else { /* Resume */
  525.                         yieldTime = 0;
  526.                         ShowWindow((WindowPtr) myWindow);
  527.                         SetPort((WindowPtr) myWindow);
  528.                         }
  529.                     break;
  530.                 default:
  531.                     break;
  532.                 }
  533.             }
  534.         }
  535. }